/** 
 * Copyright (C), 2010-2012, AIYOU Software Co.,Ltd. ALL RIGHTS RESERVED. 
 * File name:FriendsAction.java
 * Version:1.0
 * Date:2011-12-26
 * Description: 
 * author:whh 
 */
package com.tietiao.action.user;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONObject;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.tietiao.common.action.BaseAction;
import com.tietiao.common.util.PropertyUtil;
import com.tietiao.dao.model.CommodityInfo;
import com.tietiao.dao.model.Lable;
import com.tietiao.dao.model.MidUserAttention;
import com.tietiao.dao.model.SysUser;
import com.tietiao.dao.model.UserAttentionGroup;
import com.tietiao.dao.model.UserCommidityFavorite;
import com.tietiao.dao.model.UserLable;
import com.tietiao.service.commodity.CommodityService;
import com.tietiao.service.label.UserLableService;
import com.tietiao.service.miduserattention.MidUserAttentionService;
import com.tietiao.service.sysuser.SysUserService;
import com.tietiao.service.tietiao.CommidityFavoriteService;
import com.tietiao.service.userattentiongroup.UserAttentionGroupService;

@Controller
@Scope("prototype")
public class FriendsAction extends BaseAction {
	private static final long serialVersionUID = 3320290253973718088L;
	@Resource
	private MidUserAttentionService midUserAttentionService;
	@Resource
	private UserLableService userLableService;
	@Resource
	private SysUserService sysUserService;
	@Resource
	private CommodityService commodityService;
	@Resource
	private CommidityFavoriteService commidityFavoriteService;
	@Resource
	private UserAttentionGroupService userAttentionGroupService;

	private int userId;
	private int sysUserId;
	private int groupId;
	private int lableId;
	private int pageIndex = 1;// 默认列表显示数据量
	private String url = "friend_friends_favorite.htm";// 页面传回的显示类型
	private int maxCommiSize = 300;
	private String orderBy = "UPLOADER_TIME";

	private Integer pageNo;

	private Integer pageSize;

	public Integer getPageNo() {
		return pageNo;
	}

	public void setPageNo(Integer pageNo) {
		this.pageNo = pageNo;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	// 图片地址访问路径
	private String rootPicPath = PropertyUtil.getValue("PIC_SERVER");

	/**
	 * 功能：进入个人或者好友主页
	 * 
	 * @author: whh
	 * @Date: 2012-1-3
	 * @return
	 */
	public String home() {
		String requestId = request.getParameter("userId");
		int userId = requestId == null ? getUser().getUserId() : Integer.parseInt(requestId);
		request.setAttribute("sysUser", sysUserService.loadSysUserById(userId));
		// 我关注与关注我的好友数量
		int meAttenFried = sysUserService.selectFriendsUserCount(userId, null);
		request.setAttribute("meAttenFried", meAttenFried);
		int attenMeFried = sysUserService.selectFriendsUserCount(null, userId);
		request.setAttribute("attenMeFried", attenMeFried);
		// 我喜欢与喜欢我的商品
		request.setAttribute("myFavorite", commodityService.getMyFavComNum(userId));
		request.setAttribute("favoriteByOther", commodityService.getTieTiaoNum(null, userId, null));
		// 我收藏的标签
		List<UserLable> lables = userLableService.getUserLables(userId, 30);
		request.setAttribute("lables", lables);
		request.setAttribute("userId", userId);
		return DEFAULT;
	}

	public String likeMyComm() {
		Integer loginUserId = null;
		if (getUser() != null) {
			loginUserId = getUser().getUserId();
		}
		String orderBy = "UPLOADER_TIME".equals(this.orderBy) ? "UPLOADER_TIME desc" : "TIETIAO_NUM desc";
		List<CommodityInfo> list = commodityService.getTieTiaoCommodityInfo(0, maxCommiSize, null, userId, null, loginUserId, orderBy);
		request.setAttribute("commodityInfos", list);
		return DEFAULT;
	}

	/**
	 * 功能：我收藏的标签
	 * 
	 * @author: whh
	 * @Date: 2012-1-5
	 * 
	 * @return
	 */
	public String commodity() {
		if (pageNo == 1) {
			List<UserCommidityFavorite> favorites = commidityFavoriteService.getUserCommidityFavorites(userId, null);
			request.setAttribute("favorites", favorites);
		}
		Integer loginUserId = null;
		if (getUser() != null) {
			loginUserId = getUser().getUserId();
		}
		List<CommodityInfo> list = commodityService.getTieTiaoCommodityInfo((pageNo - 1) * pageSize, pageSize, userId, null, lableId, loginUserId, orderBy);
		request.setAttribute("commodityInfos", list);
		request.setAttribute("commdityLength", list.size());
		return DEFAULT;
	}

	/**
	 * 
	 * 功能：我贴条的商品页面
	 * 
	 * @author: whh
	 * @Date: 2012-1-5
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public String favorite() throws UnsupportedEncodingException {
		String orderBy = "UPLOADER_TIME".equals(this.orderBy) ? "UPLOADER_TIME DESC" : "TIETIAO_NUM DESC";
		String name = new String(request.getParameter("name").getBytes("ISO-8859-1"), "UTF-8");

		Integer loginUserId = null;
		if (getUser() != null) {
			loginUserId = getUser().getUserId();
		}
		List<CommodityInfo> list = commodityService.getMyFavoriteLableComm(0, 300, "".equals(name) ? null : name, loginUserId, userId, orderBy);
		request.setAttribute("commodityInfos", list);
		return DEFAULT;
	}

	/**
	 * 功能：我的粉丝
	 * 
	 * @author: whh
	 * @Date: 2011-12-28
	 * 
	 * @return
	 */
	public String attentionMeList() {
		SysUser sysUser = getUser();
		Lable lable = new Lable();
		lable.setUserId(sysUser.getUserId());
		List<SysUser> friend = sysUserService.selectFriendsUser(0, 150, null, userId, null, -1);
		List<String> midUserAttention = midUserAttentionService.selectFriendsUser(userId);
		request.setAttribute("friends", friend);

		request.setAttribute("midUserAttention", midUserAttention);
		int attenMeFried = sysUserService.selectFriendsUserCount(null, userId);
		request.setAttribute("attenMeFried", attenMeFried);
		// 左侧所需要数据
		return DEFAULT;
	}

	/**
	 * 
	 * 功能：我关注的好友
	 * 
	 * @author: whh
	 * @Date: 2011-12-28
	 * @return
	 */
	public String meAttentionList() {
		Lable lable = new Lable();
		lable.setUserId(userId);
		List<SysUser> friend = sysUserService.selectFriendsUser(0, 150, userId, null, null, groupId);
		List<String> midUserAttention = midUserAttentionService.selectFriendsUser(userId);

		request.setAttribute("friends", friend);
		request.setAttribute("midUserAttention", midUserAttention);
		int meAttenFried = sysUserService.selectFriendsUserCount(userId, null);
		List<UserAttentionGroup> groups = userAttentionGroupService.getUserAttentionGroup(userId);
		request.setAttribute("groups", groups);
		request.setAttribute("meAttenFried", meAttenFried);
		return DEFAULT;
	}

	/**
	 * 
	 * 功能：处理好友关系
	 * 
	 * @author: whh
	 * @Date: 2011-12-28
	 * @throws IOException
	 */
	public void dealRelation() throws IOException {
		response.setContentType("text/html");
		SysUser sysUser = getUser();
		JSONObject obj = new JSONObject();
		int result = 0;
		if ("cancel".equals(request.getParameter("type"))) {
			result = midUserAttentionService.deleteAttention(sysUser, sysUserId);
		} else {
			MidUserAttention attention = new MidUserAttention();
			attention.setAttentionTime(new Date());
			attention.setUserId(sysUser.getUserId());
			attention.setSysUserId(sysUserId);
			attention.setGroupId(groupId);
			attention.setLastAttentionTime(new Date());
			attention.setAttentionNum(0);
			result = midUserAttentionService.insertMidUserAttention(attention);
		}
		if (result > 0) {
			obj.put("result", true);
		} else {
			obj.put("result", false);
		}
		response.getWriter().append(obj.toString());
		response.getWriter().flush();
		response.getWriter().close();
	}

	/**
	 * 功能： 批量关注好友信息
	 * 
	 * @author: whh
	 * @throws IOException
	 * @Date: 2012-1-11
	 * 
	 */
	public void attenLablePerson() throws IOException {
		response.setContentType("text/html");
		SysUser sysUser = getUser();
		JSONObject obj = new JSONObject();
		obj.put("result", true);
		String[] ids = request.getParameterValues("id");

		for (int i = 0; i < ids.length; i++) {
			MidUserAttention attention = new MidUserAttention();
			attention.setAttentionTime(new Date());
			attention.setUserId(sysUser.getUserId());
			attention.setSysUserId(Integer.parseInt(ids[i]));
			attention.setGroupId(groupId);
			attention.setLastAttentionTime(new Date());
			attention.setAttentionNum(0);
			midUserAttentionService.insertMidUserAttention(attention);
		}
		response.getWriter().append(obj.toString());
		response.getWriter().flush();
		response.getWriter().close();
	}

	/**
	 * 功能：添加好友分组
	 * 
	 * @throws IOException
	 * @Date: 2012-1-7
	 * 
	 */
	public void addGroup() throws IOException {
		response.setContentType("text/html");
		SysUser sysUser = getUser();
		JSONObject obj = new JSONObject();

		List<UserAttentionGroup> list = userAttentionGroupService.getUserAttentionGroup(sysUser.getUserId());
		if (list != null && list.size() > 10) {
			obj.put("result", false);
			obj.put("message", "您已达到创建分组最大限制！");
		} else if (request.getParameter("name").length() > 10) {
			obj.put("result", false);
			obj.put("message", "分组名称过长，请重新填写！");
		} else {
			UserAttentionGroup group = new UserAttentionGroup();
			group.setCreateTime(new Date());
			group.setGroupName(request.getParameter("name"));
			group.setSecreyLevel(1);
			group.setSort(1000);
			group.setUserId(sysUser.getUserId());
			userAttentionGroupService.insertUserAttentionGroup(group);
			obj.put("result", true);
			obj.put("groupId", group.getGroupId());
			obj.put("message", "创建成功");
			System.out.println(group.getGroupId());
		}

		response.getWriter().append(obj.toString());
		response.getWriter().flush();
		response.getWriter().close();

	}

	/**
	 * 功能：转向好友分组页面
	 * 
	 * @author: whh
	 * @Date: 2012-1-7
	 * 
	 */
	public String userGroup() {
		SysUser user = getUser();
		List<UserAttentionGroup> list = userAttentionGroupService.getUserAttentionGroup(user.getUserId());
		if (list == null || list.size() == 0) {
			UserAttentionGroup group = new UserAttentionGroup();
			group.setCreateTime(new Date());
			group.setGroupName("未分类");
			group.setSecreyLevel(1);
			group.setSort(1000);
			group.setUserId(user.getUserId());
			userAttentionGroupService.insertUserAttentionGroup(group);
			list = new ArrayList<UserAttentionGroup>();
			list.add(group);
		}
		request.setAttribute("groups", list);
		return dispatcher("/jsp/fg/friend/group.jsp");
	}

	public int getUserId() {
		return userId;
	}

	public void setUserId(int userId) {
		this.userId = userId;
	}

	public int getSysUserId() {
		return sysUserId;
	}

	public void setSysUserId(int sysUserId) {
		this.sysUserId = sysUserId;
	}

	public int getGroupId() {
		return groupId;
	}

	public void setGroupId(int groupId) {
		this.groupId = groupId;
	}

	public int getPageIndex() {
		return pageIndex;
	}

	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public void setCommidityFavoriteService(CommidityFavoriteService commidityFavoriteService) {
		this.commidityFavoriteService = commidityFavoriteService;
	}

	public String getOrderBy() {
		return orderBy;
	}

	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}

	public int getLableId() {
		return lableId;
	}

	public void setLableId(int lableId) {
		this.lableId = lableId;
	}

	/**
	 * @return the rootPicPath
	 */
	public String getRootPicPath() {
		return rootPicPath;
	}

	/**
	 * @param rootPicPath
	 *            the rootPicPath to set
	 */
	public void setRootPicPath(String rootPicPath) {
		this.rootPicPath = rootPicPath;
	}
}
